package com.qo.qsdk.ui.base.floater.animation;

import android.app.Activity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import com.qo.qsdk.ui.base.floater.ctrl.AssistorView;
import com.qo.qsdk.ui.base.floater.parameters.AssistorViewParameters;
import com.qo.qsdk.utils.QSDKHelper;

import java.util.ArrayList;

public class AssistorViewMoveToSideAnimation {
    private static final String TAG = AssistorViewMoveToSideAnimation.class.getSimpleName();

    public AssistorPointState mAssistorPointState = AssistorPointState.DEFAULT;
    private AssistorView mAssistorView;

    private AssistorViewParameters mParameters;
    private ArrayList<Animation> mAnimationArrayList;
    private boolean mDisableMoveToSideAnimation = false;
    private boolean mIsImageButtonPlaceRight = false;
    private int mFlag = 0;

    private AnimationListenerWrapper mAnimationListenerWrapper =
            new AnimationListenerWrapper(new AnimationNotifier<Animation>() {
                @Override
                public void onAnimation(AnimationState state, Animation animation) {
                    if (AnimationState.END == state) {
                        update(mActivity);
                    }
                }
            });

    private enum AssistorPointState {
        DEFAULT,
        HALF_PART;

        public static AssistorPointState factory(String str) {
            return Enum.valueOf(AssistorPointState.class, str);
        }
    }

    public AssistorViewMoveToSideAnimation(AssistorView assistorView,
                                           AssistorViewParameters parameters,
                                           boolean disableMoveToSideAnimation) {
        mAssistorView = assistorView;

        mParameters = parameters;
        mDisableMoveToSideAnimation = disableMoveToSideAnimation;
    }

    private void setNormalBackgroundDrawable(Activity activity, String str) {
        if (mAssistorView != null) {
            mAssistorView.setNormalBackgroundDrawable(activity, str);
        }
    }

    private void update(Activity activity) {
        if (mAssistorPointState == AssistorPointState.HALF_PART && mAnimationArrayList != null &&
                mAnimationArrayList.size() > mFlag && mAssistorView != null) {
            mAssistorView.startAnimation(mAnimationArrayList.get(mFlag));
            mFlag++;
        } else if (mFlag > 0 && mFlag >= mAnimationArrayList.size()) {
            //
        } else if (mAnimationArrayList == null || mAssistorView == null) {
            clearMoveToSideAnimation();
            setNormalBackgroundDrawable(activity, mParameters.getNormalIcon());
        }
    }

    private void setAssistorPointPlaceNONE() {
        mAssistorView.mPlace = AssistorView.Place.NONE;
    }

    private void initAnimationArrayList() {
        mAnimationArrayList = new ArrayList<Animation>();
        float xValueDirection;
        float xDeltaDirection;
        final String slideIconA;
        final String slideIconB;
        if (mIsImageButtonPlaceRight) {
            xValueDirection = 0.0f;
            xDeltaDirection = 1.0f;
            slideIconA = mParameters.getSlideIconRightA();
            slideIconB = mParameters.getSlideIconRightB();
        }
        else {
            xValueDirection = 1.0f;
            xDeltaDirection = -1.0f;
            slideIconA = mParameters.getSlideIconLeftA();
            slideIconB = mParameters.getSlideIconLeftB();
        }

        final float assistorViewWidth = mAssistorView.getWidth();
        if (mDisableMoveToSideAnimation) {
            final AnimationSet set = new AnimationSet(true);
            set.setFillEnabled(true);
            set.setFillAfter(true);
            set.setFillBefore(true);
            final TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, xDeltaDirection * assistorViewWidth / 2.0f, 0.0f, 0.0f);
            translateAnimation.setAnimationListener(
                    new AnimationListenerWrapper(new AnimationNotifier<Animation>() {
                        @Override
                        public void onAnimation(AnimationState state, Animation animation) {
                            if (AnimationState.END == state) {
                                setNormalBackgroundDrawable(mActivity, slideIconA);
                            }
                        }
                    }));
            set.addAnimation(translateAnimation);
            set.addAnimation(new AlphaAnimation(0.0f, 0.5f));
            set.setAnimationListener(mAnimationListenerWrapper);
            mAnimationArrayList.add(set);
            return;
        }

        {
            final ScaleAnimation animation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 1.0f, 1, xValueDirection, 1, 0.0f);
            animation.setDuration(200L);
            animation.setFillAfter(true);
            animation.setAnimationListener(mAnimationListenerWrapper);
            mAnimationArrayList.add(animation);
        }

        {
            final TranslateAnimation animation = new TranslateAnimation(0.0f, xDeltaDirection * assistorViewWidth / 2.0f, 0.0f, 0.0f);
            animation.setDuration(37L);
            animation.setFillAfter(true);
            animation.setAnimationListener(mAnimationListenerWrapper);
            mAnimationArrayList.add(animation);
        }

        {
            final TranslateAnimation animation = new TranslateAnimation(xDeltaDirection * assistorViewWidth / 2.0f, xDeltaDirection * assistorViewWidth, 0.0f, 0.0f);
            (animation).setDuration(37L);
            (animation).setFillAfter(true);
            (animation).setAnimationListener(
                    new AnimationListenerWrapper(new AnimationNotifier<Animation>() {
                        @Override
                        public void onAnimation(AnimationState state, Animation animation) {
                            if (AnimationState.END == state) {
                                setNormalBackgroundDrawable(mActivity, slideIconA);
                                update(mActivity);
                            }
                        }
                    }));
            mAnimationArrayList.add(animation);
        }

        {
            final float w = assistorViewWidth / 8.0f;
            final TranslateAnimation animation = new TranslateAnimation(xDeltaDirection * assistorViewWidth, (assistorViewWidth / 2.0f - w) * xDeltaDirection, 0.0f, 0.0f);
            animation.setDuration(50L);
            animation.setFillAfter(true);
            animation.setAnimationListener(mAnimationListenerWrapper);
            mAnimationArrayList.add(animation);
        }

        {
            final float w = assistorViewWidth / 8.0f;
            final TranslateAnimation animation = new TranslateAnimation((assistorViewWidth / 2.0f - w) * xDeltaDirection, (assistorViewWidth / 2.0f + w / 2.0f) * xDeltaDirection, 0.0f, 0.0f);
            animation.setDuration(50L);
            animation.setFillAfter(true);
            animation.setAnimationListener(mAnimationListenerWrapper);
            mAnimationArrayList.add(animation);
        }

        {
            final AnimationSet set = new AnimationSet(true);
            set.setFillEnabled(true);
            set.setFillAfter(true);
            set.setFillBefore(true);

            {
                final float w = assistorViewWidth / 8.0f;
                final TranslateAnimation animation = new TranslateAnimation((w / 2.0f + assistorViewWidth / 2.0f) * xDeltaDirection, assistorViewWidth / 2.0f * xDeltaDirection, 0.0f, 0.0f);
                animation.setDuration(50L);
                animation.setAnimationListener(
                        new AnimationListenerWrapper(new AnimationNotifier<Animation>() {
                            @Override
                            public void onAnimation(AnimationState state, Animation animation) {
                                if (AnimationState.END == state) {
                                    QSDKHelper.getHandler().postDelayed((Runnable) new Runnable() {
                                        @Override
                                        public void run() {
                                            if (mAssistorPointState == AssistorPointState.HALF_PART) {
                                                setNormalBackgroundDrawable(mActivity, slideIconB);
                                                QSDKHelper.getHandler().postDelayed((Runnable) new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        if (mAssistorPointState == AssistorPointState.HALF_PART) {
                                                            setNormalBackgroundDrawable(mActivity, slideIconA);
                                                        }
                                                    }
                                                }, 150L);
                                            }
                                        }
                                    }, 300L);
                                }
                            }
                        }));
                set.addAnimation(animation);
            }

            {
                final AlphaAnimation animation = new AlphaAnimation(1.0f, 0.5f);
                (animation).setDuration(300L);
                (animation).setStartOffset(1200L);
                set.addAnimation(animation);
            }

            set.setAnimationListener(mAnimationListenerWrapper);
            mAnimationArrayList.add(set);
        }
    }

    public void clearMoveToSideAnimation() {
        if (mAssistorView != null && mAssistorPointState == AssistorPointState.HALF_PART) {
            mAssistorPointState = AssistorPointState.DEFAULT;
            mAssistorView.clearAnimation();
            mFlag = 0;
            mAnimationArrayList = null;
            setAssistorPointPlaceNONE();
        }
    }

    public void disableMoveToSideAnimation(boolean disable_move_to_side_animation) {
        mDisableMoveToSideAnimation = disable_move_to_side_animation;
    }

    private Activity mActivity;
    public void startMoveToSideAnimation(Activity activity, final boolean placeRight) {
        mActivity = activity;
        if (mAssistorPointState == AssistorPointState.DEFAULT) {
            if (mIsImageButtonPlaceRight != placeRight) {
                mIsImageButtonPlaceRight = placeRight;
                mAnimationArrayList = null;
                initAnimationArrayList();
            } else if (mAnimationArrayList == null) {
                initAnimationArrayList();
            }

            QSDKHelper.getHandler().post(new Runnable() {
                @Override
                public void run() {
                    mAssistorPointState = AssistorPointState.HALF_PART;
                    if (mIsImageButtonPlaceRight) {
                        mAssistorView.mPlace = AssistorView.Place.RIGHT;
                    }
                    else {
                        mAssistorView.mPlace = AssistorView.Place.LEFT;
                    }

                    update(mActivity);
                }
            });
        }
    }
}
